﻿// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk
{
	using System;
	using System.Runtime.Serialization;
	using Microsoft.Xrm.Sdk.Temporary;

	/// <summary>
	/// Pointer to an entity instance
	/// </summary>
	[DataContract(Name = "EntityReference", Namespace = XmlNamespaces.V5.Contracts)]
	[Serializable]
	public sealed class EntityReference : IExtensibleDataObject
	{
		public EntityReference()
		{
		}
		public EntityReference(string logicalName, Guid id)
		{
			_logicalName = logicalName;
			_id = id;
		}

		[DataMember]
		public Guid Id
		{
			get { return _id; }
			set { _id = value; }
		}

		[DataMember]
		public string LogicalName
		{
			get { return _logicalName; }
			set { _logicalName = value; }
		}

		[DataMember]
		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public override bool Equals(object obj)
		{
			EntityReference other = obj as EntityReference;
			if (other == null)
			{
				return false;
			}
			if (this == other)
			{
				return true;
			}
			return _id.Equals(other._id) && (string.Compare(_logicalName, other._logicalName, StringComparison.Ordinal) == 0);
		}

		public override int GetHashCode()
		{
			return string.IsNullOrEmpty(_logicalName) ? 0 : _logicalName.GetHashCode() ^ _id.GetHashCode();
		}

		private string _logicalName;
		private string _name;
		private Guid _id;


		#region IExtensibleDataObject Implementation
		[NonSerialized]
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}
